///*
// * Copyright (C) 2014 pengjianbo(pengjianbosoft@gmail.com), Inc.
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// *     http://www.apache.org/licenses/LICENSE-2.0
// *
// *  Unless required by applicable law or agreed to in writing, software
// *  distributed under the License is distributed on an "AS IS" BASIS,
// *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// *  See the License for the specific language governing permissions and
// *  limitations under the License.
// */
//
//package com.qunar.im.ui.imagepicker.loader;
//
//import android.app.Activity;
//import android.content.Context;
//import android.content.res.Resources;
//import android.graphics.Bitmap;
//import android.graphics.Canvas;
//import android.graphics.drawable.Drawable;
//import android.net.Uri;
//import android.widget.ImageView;
//
//import com.facebook.common.util.UriUtil;
//import com.facebook.drawee.backends.pipeline.Fresco;
//import com.facebook.drawee.drawable.ProgressBarDrawable;
//import com.facebook.drawee.generic.GenericDraweeHierarchy;
//import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
//import com.facebook.drawee.interfaces.DraweeController;
//import com.facebook.drawee.view.DraweeHolder;
//import com.facebook.imagepipeline.common.ResizeOptions;
//import com.facebook.imagepipeline.core.ImagePipelineConfig;
//import com.facebook.imagepipeline.request.ImageRequest;
//import com.facebook.imagepipeline.request.ImageRequestBuilder;
//import com.qunar.im.ui.imagepicker.view.GFImageView;
//
//
///**
// *
// */
//public class FrescoImageLoader implements ImageLoader {
//
//    private Context context;
//
//    public FrescoImageLoader(Context context) {
//        this(context, Bitmap.Config.RGB_565);
//    }
//
//    public FrescoImageLoader(Context context, Bitmap.Config config) {
//        this.context = context;
//        ImagePipelineConfig imagePipelineConfig = ImagePipelineConfig.newBuilder(context)
//                .setBitmapsConfig(config)
//                .build();
//        Fresco.initialize(context, imagePipelineConfig);
//    }
//
//    @Override
//    public void displayImage(Activity activity, String path, final GFImageView imageView, final Drawable defaultDrawable, int width, int height) {
//        Resources resources = context.getResources();
//        GenericDraweeHierarchy hierarchy = new GenericDraweeHierarchyBuilder(resources)
//                .setFadeDuration(300)
//                .setPlaceholderImage(defaultDrawable)
//                .setFailureImage(defaultDrawable)
//                .setProgressBarImage(new ProgressBarDrawable())
//                .build();
//
//        final DraweeHolder<GenericDraweeHierarchy> draweeHolder = DraweeHolder.create(hierarchy, context);
//        imageView.setOnImageViewListener(new GFImageView.OnImageViewListener() {
//            @Override
//            public void onDetach() {
//                draweeHolder.onDetach();
//            }
//
//            @Override
//            public void onAttach() {
//                draweeHolder.onAttach();
//            }
//
//            @Override
//            public boolean verifyDrawable(Drawable dr) {
//                if (dr == draweeHolder.getHierarchy().getTopLevelDrawable()) {
//                    return true;
//                }
//                return false;
//            }
//
//            @Override
//            public void onDraw(Canvas canvas) {
//                Drawable drawable = draweeHolder.getHierarchy().getTopLevelDrawable();
//                if (drawable == null) {
//                    imageView.setImageDrawable(defaultDrawable);
//                } else {
//                    imageView.setImageDrawable(drawable);
//                }
//            }
//        });
//        Uri uri = new Uri.Builder()
//                .scheme(UriUtil.LOCAL_FILE_SCHEME)
//                .path(path)
//                .build();
//        ImageRequest imageRequest = ImageRequestBuilder
//                .newBuilderWithSource(uri)
//                .setResizeOptions(new ResizeOptions(width, height))//图片目标大小
//                .build();
//        DraweeController controller = Fresco.newDraweeControllerBuilder()
//                .setOldController(draweeHolder.getController())
//                .setImageRequest(imageRequest)
//                .build();
//        draweeHolder.setController(controller);
//    }
//
//    @Override
//    public void displayImagePreview(Activity activity, String path, ImageView imageView, Drawable defaultDrawable, int width, int height) {
//        Resources resources = context.getResources();
//        GenericDraweeHierarchy hierarchy = new GenericDraweeHierarchyBuilder(resources)
//                .setFadeDuration(300)
//                .setPlaceholderImage(defaultDrawable)
//                .setFailureImage(defaultDrawable)
//                .setProgressBarImage(new ProgressBarDrawable())
//                .build();
//
//        final DraweeHolder<GenericDraweeHierarchy> draweeHolder = DraweeHolder.create(hierarchy, context);
////        imageView.setOnImageViewListener(new GFImageView.OnImageViewListener() {
////            @Override
////            public void onDetach() {
////                draweeHolder.onDetach();
////            }
////
////            @Override
////            public void onAttach() {
////                draweeHolder.onAttach();
////            }
////
////            @Override
////            public boolean verifyDrawable(Drawable dr) {
////                if (dr == draweeHolder.getHierarchy().getTopLevelDrawable()) {
////                    return true;
////                }
////                return false;
////            }
////
////            @Override
////            public void onDraw(Canvas canvas) {
////                Drawable drawable = draweeHolder.getHierarchy().getTopLevelDrawable();
////                if (drawable == null) {
////                    imageView.setImageDrawable(defaultDrawable);
////                } else {
////                    imageView.setImageDrawable(drawable);
////                }
////            }
////        });
//        Uri uri = new Uri.Builder()
//                .scheme(UriUtil.LOCAL_FILE_SCHEME)
//                .path(path)
//                .build();
//        ImageRequest imageRequest = ImageRequestBuilder
//                .newBuilderWithSource(uri)
//                .setResizeOptions(new ResizeOptions(width, height))//图片目标大小
//                .build();
//        DraweeController controller = Fresco.newDraweeControllerBuilder()
//                .setOldController(draweeHolder.getController())
//                .setImageRequest(imageRequest)
//                .build();
//        draweeHolder.setController(controller);
//    }
//
//    @Override
//    public void clearMemoryCache() {
//
//    }
//}
